package controller.pub;

import controller.MyCoolServlet;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.*;
import javax.servlet.http.*;
import model.Product;
import persistence.product.ProductDAO;
import persistence.product.ProductPersistFactory;

public class ReadServlet extends MyCoolServlet {

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ProductDAO persistenceManager = ProductPersistFactory.getProductDAO(persistenceMechanism);
        String name = request.getParameter("name");
        Map<String, Product> productMap = new HashMap<String, Product>();
        persistenceManager.readProduct(name, productMap);
        Product p = productMap.get(name+"_iOS");
        if (productMap != null && !productMap.isEmpty()) {
            request.setAttribute("productMap", productMap);
            Iterator itr = productMap.entrySet().iterator();
            request.setAttribute("itr",itr);
            gotoURL(displayForm, request, response);
        } else {
            request.setAttribute("message1", "Error al buscar la app:");
            request.setAttribute("message2", "Es posible que no exista dicha app");
            gotoURL(errorForm, request, response);
        }
    }
}
